package display;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.HashMap;

import service.TrackManager;
import domain.Track;

//import service.CDManager;
//import domain.Grupo;

@Controller
@RequestMapping("/track")
public class TrackController {
 
 @Autowired
 private TrackManager trackManager;
 
 //@Autowired
 //private CDMnager CDManager;
 
 @RequestMapping(value="/listado", method = RequestMethod.GET)
 public ModelAndView listado() {

   Map<String, Object> myModel = new HashMap<String, Object>();
   myModel.put("tracks", this.trackManager.getAll());
   return new ModelAndView("listaTracks", "model", myModel);

 }
 
 @RequestMapping(value="/detalleTrack/{pk_track}", method = RequestMethod.GET)
 public ModelAndView detalleTrack(@PathVariable("pk_track") String id) {

   ModelAndView modelAndView = new ModelAndView();
   Track element = this.trackManager.getById(id);

   if (element == null){
     System.out.println("NULO");
	}else{ 
    modelAndView = new ModelAndView("detalleTrack");
    modelAndView.addObject("track", element);
		//Map<String, Object> myModel = new HashMap<String, Object>();
    //myModel.put("CDs", this.CDManager.getAllByBandaID(id));
		//modelAndView.addObject("model", myModel);
   }
    return modelAndView;	 
 }
 

 @RequestMapping(value="/actualizarTrack/{pk_track}", params = { "pk_track","fk_CD","duracion","autor","nombre" }, method = RequestMethod.GET)
 public String actualizarTrack(
     @PathVariable("pk_track") String pk_track, 
     @RequestParam("fk_CD") String fk_CD,
     @RequestParam("duracion") String duracion,
     @RequestParam("autor") String autor,
     @RequestParam("nombre") String nombre
     
  ) {
   Track element = this.trackManager.getById(pk_track);
   element.setFk_CD(fk_CD);
   element.setDuracion(Integer.parseInt(duracion));
   element.setAutor(autor);
   element.setNombre(nombre);   
   return "forward:/track/listado";
 }
 
 @RequestMapping(value="/agregarTrack", method = RequestMethod.GET)
 public ModelAndView aregar() {
 	return new ModelAndView("agregarTrack");
 }
 


 @RequestMapping(value="/insertarTrack", params = { "fk_CD","duracion","autor","nombre"}, method = RequestMethod.GET)
 public String actualizar(
     @RequestParam("fk_CD") String fk_CD,
     @RequestParam("duracion") String duracion,
     @RequestParam("autor") String autor,
     @RequestParam("nombre") String nombre
  ) {

   Track element = new Track();
   element.setFk_CD(fk_CD);
   element.setDuracion(Integer.parseInt(duracion));
   element.setAutor(autor);
   element.setNombre(nombre);
   this.trackManager.insert(element);
   return "forward:/track/listado";
 }
 
  @RequestMapping(value="/eliminarTrack/{pk_track}", method = RequestMethod.GET)
 public String eliminar(@PathVariable("pk_track") String id) {
   if (id != null){
	   this.trackManager.delete(id);
   }
   return "forward:/track/listado";
 } 

 public void setTrackManager(TrackManager trackManager) {
   this.trackManager = trackManager;
 } 
}